package com.zzzy.sys.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzzy.common.vo.PageResult;
import com.zzzy.framework.common.annotation.DataPermission;
import com.zzzy.framework.core.mybatis.query.MPJLambdaWrapperE;
import com.zzzy.sys.dao.entity.User;
import com.zzzy.sys.dao.entity.UserLoginLog;
import com.zzzy.sys.dao.mapper.UserLoginLogMapper;
import com.zzzy.sys.vo.log.UserLoginLogPageReq;
import com.zzzy.sys.vo.log.UserLoginLogRes;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 用户登录日志表 服务实现类
 *
 * @author fc
 * @since 2024-04-19
 */
@Service
public class UserLoginLogService extends ServiceImpl<UserLoginLogMapper, UserLoginLog> {

    @DataPermission
    public PageResult<UserLoginLogRes> getUserLoginLogPage(UserLoginLogPageReq queryReq) {
        MPJLambdaWrapperE<UserLoginLog> queryWrapperE = new MPJLambdaWrapperE<>();
        queryWrapperE.select(UserLoginLog::getId, UserLoginLog::getUserId, UserLoginLog::getLoginTime, UserLoginLog::getIp);
        queryWrapperE.selectAs(User::getNickName, UserLoginLogRes::getNickName);

        queryWrapperE.innerJoin(User.class, User::getId, UserLoginLog::getUserId)
                .likeIfExists(User::getNickName, queryReq.getNickName());

        queryWrapperE.betweenIfPresent(UserLoginLog::getLoginTime, queryReq.getBeginTime(), Objects.nonNull(queryReq.getEndTime()) ? queryReq.getEndTime().plusDays(1) : null)
                .orderByDesc(UserLoginLog::getId);
        return baseMapper.selectJoinPage(queryReq, UserLoginLogRes.class, queryWrapperE);
    }
}
